/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package statements;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.faces.model.DataModel;
import javax.persistence.Query;
import jpa.entities.Merchants;
import jpa.entities.MerchantsFacade;
import jsf.MerchantsController;
import statements.beans.CCStatementCSV;

/**
 *
 * @author bsmith
 */
public class StatementLoader {

    private List<InputStream> dataInputStreams;
    private List<CCStatementCSV> dataList;
    private MerchantsFacade ejbMerchantsFacade;

    public StatementLoader() {
    }

    public StatementLoader(List<InputStream> dataInputStreams, MerchantsFacade merchantsFacade) throws IOException {
        this.dataInputStreams = dataInputStreams;
        this.ejbMerchantsFacade = merchantsFacade;
        parseData();
        printData();
    }

    public static List<InputStream> getSampleData() {
        List<InputStream> list = new ArrayList<InputStream>();
        final String resourcesPath = "resources/sampledata01.csv";
        InputStream stream = StatementLoader.class.getResourceAsStream(resourcesPath);
        System.out.println("Stream is: " + stream.toString());
        list.add(stream);
        return list;
    }

    private void parseData() throws IOException {
        InputStream is = dataInputStreams.get(0);
        CSVReader reader = new CSVReader(new InputStreamReader(is));
        ColumnPositionMappingStrategy<CCStatementCSV> strat = new ColumnPositionMappingStrategy<CCStatementCSV>();
        strat.setType(CCStatementCSV.class);
        String[] columns = new String[]{"Date", "Amount", "Id", "Unknown", "Type", "Merchant", "Balance"}; // the fields to bind do in your JavaBean
        strat.setColumnMapping(columns);

        String[] line;

        CsvToBean<CCStatementCSV> csv = new CsvToBean<CCStatementCSV>();
        this.dataList = csv.parse(strat, reader);

        //MerchantsController mc = new MerchantsController();
        //DataModel merchantItems = mc.getItems();
        EntityManager em = ejbMerchantsFacade.getEntityManager();
        Query q = em.createNamedQuery("Merchants.findAll");
        List<Merchants> merchants = q.getResultList();//findAll();
        //List<Merchants> merchants = mf.findAll();
        for (Merchants m : merchants) {
            System.out.println("Merchant: " + m);
            Query q2 = em.createNamedQuery("Merchants.findById");
            q2.setParameter("id", m.getId());
            List<Merchants> merchants2 = q2.getResultList();//findAll();
            for (Merchants m2 : merchants2) {
                System.out.println("Merchant2: " + m2.getId() + ", "+m2.getMerchant()+", "+m2.getStore());
            }
        }
        Query q3 = em.createNamedQuery("Merchants.findAllData");
        List<Merchants> merchants3 = q.getResultList();//findAll();
        for (Merchants m3 : merchants3) {
            System.out.println("Merchant3: " + m3.getId() + ", "+m3.getMerchant()+", "+m3.getStore());
        }
    }

    public List<CCStatementCSV> getDataList() {
        return this.dataList;
    }

    private void printData() {
        for (Object item : this.dataList) {
            CCStatementCSV ccs = (CCStatementCSV) item;
            System.out.println("  CCStatement:" + ccs.toString());
        }
    }
}
